import Direction from './direction.js'
import MoveableEntity from './Moveable.js'
import Event from './event.js'

export default class Rocket extends MoveableEntity {
  constructor(owner = {}, x, y, width = 14, height = 14, imgSrc = '../assets/rocket.png', life = 999, direction = Direction.DOWN, speed = 5) {
    super(x, y, width, height, imgSrc, life, direction, speed)
    this.owner = owner
    this.life = this.attack = owner.attack
    this.sprites = ['./assets/rocket.png', './assets/rocket.png', './assets/rocket.png', './assets/rocket.png']
    this.minX = -2 * this.width
    this.minY = -2 * this.height
    this.maxX = 800 + 2 * this.width
    this.maxY = 600 + 2 * this.height
    this.x = owner.x + owner.width / 2 - this.width / 2
    this.y = owner.y + owner.height / 2 - this.height / 2
    this.direction = owner.direction
  }

  autoMove(cols) {
    this.move(this.direction, cols)

    if (this.passable) {
      this.life = -1
      this.passable.takeDamage(this)
    }

    if (this.x <= this.minX + this.width || this.y <= this.minY + this.height || this.x >= this.maxX - this.width || this.y >= this.maxY - this.height) {
      this.life = -1
    }
    if (this.life <= 0) {
      this.event.trigger('onDestory', null, this)
    }
  }
}
